<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="成果名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入成果名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入作者名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="作者类别" prop="authorType">
        <el-select v-model="queryParams.authorType" placeholder="请选择作者类别" clearable>
          <el-option
            v-for="dict in dict.type.author_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="成果名称" align="center" prop="title" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="作者类别" align="center" prop="authorType">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.author_type" :value="scope.row.authorType"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="部门" align="center" prop="deptId" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_unit" :value="scope.row.deptId"/>
        </template>
      </el-table-column>
      <el-table-column label="成果类别" align="center" prop="resultType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_type" :value="scope.row.resultType"/>
        </template>
      </el-table-column>
      <el-table-column label="申请状态" align="center" prop="applyStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.applyStatus==0||scope.row.applyStatus==null" style="border: 1px solid red;padding: 1px;color: red">未申请</span>
          <span v-else-if="scope.row.applyStatus==1" style="border: 1px solid #1441f3;padding: 1px;color: #1441f3">已申请</span>
          <span v-else-if="scope.row.applyStatus==2" style="border: 1px solid #12f30a;padding: 1px;color: #12f30a">已授权</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.applyStatus==0||scope.row.applyStatus==null"
            icon="el-icon-s-promotion"
            @click="handleApply(scope.row)"
          >申请查看</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.applyStatus==2"
            icon="el-icon-search"
            @click="handleLook(scope.row)"
          >查看</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            v-if="scope.row.applyStatus==0||scope.row.applyStatus==null"-->
<!--            icon="el-icon-s-promotion"-->
<!--            @click="handleApply(scope.row)"-->
<!--          >申请下载</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            v-if="scope.row.applyStatus==2"-->
<!--            icon="el-icon-download"-->
<!--            @click="handleDownload(scope.row)"-->
<!--          >下载</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="成果查看" :visible.sync="open" width="1200px" append-to-body>
      <div>
        <span><award-detail :id="resultId" v-if="resultTypeId==1"></award-detail></span>
        <span><paper-detail :id="resultId" v-if="resultTypeId==2"></paper-detail></span>
        <span><patent-detail :id="resultId" v-if="resultTypeId==3"></patent-detail></span>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleDownload">导 出</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listOthersResult} from "@/api/work/othersResult";
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/work/record";
import user from "../../store/modules/user";
import {listUser} from "../../api/system/user";
import AwardDetail from "@/views/work/award/Detail/index.vue";
import PatentDetail from "@/views/work/patent/Detail/index.vue";
import paperDetail from "@/views/work/paper/Detail/index"

export default {
  name: "Award",
  components: {AwardDetail,PatentDetail,paperDetail},
  dicts: ['work_type','work_unit','author_type', 'unit_ranking', 'work_award_grade', 'work_award_level', 'sys_yes_no', 'sys_user_sex', 'school_audit_status', 'subject_category', 'faculty_audit_status', 'education_backgorund', 'professional_title'],
  data() {
    return {
      userID:user.state.id,
      resultId:'',
      resultTypeId:0,
      //申请状态
      // applyStatus:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWorkAwardWinners: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 获奖表格数据
      dataList: [],
      userList:[],
      recordList:[],
      // 获奖完成人表格数据
      workAwardWinnersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 职称时间范围
      daterangeAwardDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        name: null,
        gender: null,
        authorType: null,
        resultType: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询获奖列表 */
    async getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAwardDate && '' != this.daterangeAwardDate) {
        this.queryParams.params["beginAwardDate"] = this.daterangeAwardDate[0];
        this.queryParams.params["endAwardDate"] = this.daterangeAwardDate[1];
      }
      let response = await  listOthersResult(this.queryParams)
          this.dataList = response.rows;

          this.recordList = (await listRecord()).rows;
          this.userList = (await  listUser()).rows

          for (const data of this.dataList) {
            for (const user of this.userList) {
                if(data.userId==user.userId){
                  data.name=user.nickName;
                  data.gender=user.sex;
                  data.deptId=user.deptId;
                }
            }
            for (const re of this.recordList) {
              if(re.reapplyId==data.userId &&re.applyId==this.userID&&re.applyTitle==data.title&&re.applyStatus!=0){
                data.applyStatus=re.applyStatus;
              }
            }
          }
          this.total = response.total;
          this.loading = false;

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        name: null,
        gender: null,
        authorType: null,
        resultType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams)
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAwardDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 获奖完成人序号 */
    rowWorkAwardWinnersIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 复选框选中数据 */
    handleWorkAwardWinnersSelectionChange(selection) {
      this.checkedWorkAwardWinners = selection.map(item => item.index)
    },
  handleApply(row){
    let isH=0;
    let form = {}
    for (const re of this.recordList) {
      if(re.reapplyId==row.userId&&re.applyId==this.userID&&re.applyTitle==row.title){
        form=re;
        isH = 1;
      }else {
        form.reapplyId = row.userId;
        form.applyId = this.userID;
        form.applyTitle = row.title;
        form.applyStatus = 1;
        form.downloadStatus = 0;
        form.downloadCount = 0;
      }
    }
    if(!isH){
      addRecord(form).then(res =>{
        this.$modal.msgSuccess("申请成功");
        row.applyStatus= 1;
      })
    }else {
      form.applyStatus = 1;
      updateRecord(form);
    }
    this.getList();

  },
    handleDownload(row){

    },
    handleLook(row){
      this.open = true;
      this.resultId=row.id;
      this.resultTypeId=row.resultType;
    },
  }
};
</script>
